//Create SellerStampDutyView function
function SellerStampDutyView(vWidth) {
    var self = Titanium.UI.createView({
        backgroundColor: 'transparent',
    });
    var PKTextField = require('view/PickerTextField');
    var section1 = Ti.UI.createTableViewSection({
        height: 'auto',
        headerTitle: 'Seller\'s Stamp Duty'
    });
    var section2 = Ti.UI.createTableViewSection({
        height: 'auto',
        headerTitle: 'Seller\'s Stamp Duty Result'
    });
    var row1 = Ti.UI.createTableViewRow({
        height: 'auto'
    });
    var row2 = Ti.UI.createTableViewRow({
        height: 'auto'
    });
    var row3 = Ti.UI.createTableViewRow({
        height: 'auto'
    });
    var row4 = Ti.UI.createTableViewRow({
        height: 'auto'
    });
    var row5 = Ti.UI.createTableViewRow({
        height: 'auto'
    });
    var row6 = Ti.UI.createTableViewRow({
        height: 'auto'
    });
    var lblPropertyType = Titanium.UI.createLabel({
        text: 'Type of Property',
        font: {
            fontSize: 20
        },
        color: 'white',
        left: 10,
        width: vWidth / 2 - 2 * 10
    });
    var pkPropertyTypeData = [];
    pkPropertyTypeData[0] = Ti.UI.createPickerRow({
        title: 'Residential',
        valueId: 0
    });
    pkPropertyTypeData[1] = Ti.UI.createPickerRow({
        title: 'Commercial',
        valueId: 1
    });
    var txtPropertyType = new PKTextField(self, 1, pkPropertyTypeData);
    txtPropertyType.addEventListener('pickerTextFieldChange', function (e) {
        valuePropertyType = e.valueId;
        Calc();
    });
    txtPropertyType.left = vWidth / 2;
    txtPropertyType.width = vWidth / 2 - 10;
    var lblSellingPrice = Titanium.UI.createLabel({
        text: 'Selling Price include GST',
        font: {
            fontSize: 20
        },
        color: 'white',
        left: 10,
        width: vWidth / 2 - 2 * 10
    });
    var txtSellingPrice = Ti.UI.createTextField({
        left: vWidth / 2,
        right: 10,
        hintText: '',
        textAlign: "right",
        borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
        keyboardType: Titanium.UI.KEYBOARD_DECIMAL_PAD,
        width: vWidth / 2 - 10
    });
    txtSellingPrice.addEventListener('change', function (e) {
        Calc();
    });
    txtSellingPrice.addEventListener('return', function (e) {
        txtSellingPrice.blur();
    });
    var lblOriginalPurchaseDate = Titanium.UI.createLabel({
        text: 'Exercise Date of Original Purchase',
        font: {
            fontSize: 20
        },
        color: 'white',
        left: 10,
        width: vWidth / 2 - 2 * 10
    });
    var txtOriginalPurchaseDate = Ti.UI.createTextField({
        left: vWidth / 2,
        right: 10,
        hintText: '',
        textAlign: "right",
        borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
        width: vWidth / 2 - 10
    });
    txtOriginalPurchaseDate.addEventListener('focus', function (e) {
        txtOriginalPurchaseDate.blur();
        showKeyBoard(1);
    });
    txtOriginalPurchaseDate.addEventListener('return', function (e) {
        txtOriginalPurchaseDate.blur();
    });
    var lblCurrentOtpDate = Titanium.UI.createLabel({
        text: 'Exercise Date of Current OTP/S&P',
        font: {
            fontSize: 20
        },
        color: 'white',
        left: 10,
        width: vWidth / 2 - 2 * 10
    });
    var txtCurrentOtpDate = Ti.UI.createTextField({
        left: vWidth / 2,
        right: 10,
        hintText: '',
        textAlign: "right",
        borderStyle: Ti.UI.INPUT_BORDERSTYLE_ROUNDED,
        width: vWidth / 2 - 10
    });
    txtCurrentOtpDate.addEventListener('focus', function (e) {
        txtCurrentOtpDate.blur();
        showKeyBoard(2);
    });
    txtCurrentOtpDate.addEventListener('return', function (e) {
        txtCurrentOtpDate.blur();
    });
    var lblTitle1 = Titanium.UI.createLabel({
        text: 'Total Seller\'s Stamp Duty',
        font: {
            fontSize: 20
        },
        color: 'white',
        left: 10,
        width: vWidth / 2 - 2 * 10
    });
    var lblTotalSellerDuty = Titanium.UI.createLabel({
        text: 'v1',
        font: {
            fontSize: 20
        },
        color: 'red',
        left: vWidth / 2,
        width: vWidth / 2 - 2 * 10,
        textAlign: 'right'
    });
    var lblTitle2 = Titanium.UI.createLabel({
        text: 'Duration',
        font: {
            fontSize: 20
        },
        color: 'white',
        left: 10,
        width: vWidth / 2 - 2 * 10
    });
    var lblDuration = Titanium.UI.createLabel({
        text: 'v1',
        font: {
            fontSize: 20
        },
        color: 'red',
        left: vWidth / 2,
        width: vWidth / 2 - 2 * 10,
        textAlign: 'right'
    });
    row1.add(lblPropertyType);
    row1.add(txtPropertyType);
    row2.add(lblSellingPrice);
    row2.add(txtSellingPrice);
    row3.add(lblOriginalPurchaseDate);
    row3.add(txtOriginalPurchaseDate);
    row4.add(lblCurrentOtpDate);
    row4.add(txtCurrentOtpDate);
    row5.add(lblTitle1);
    row5.add(lblTotalSellerDuty);
    row6.add(lblTitle2);
    row6.add(lblDuration);
    section1.add(row1);
    section1.add(row2);
    section1.add(row3);
    section1.add(row4);
    section2.add(row5);
    section2.add(row6);
    var data = [section1, section2];
    var table = Ti.UI.createTableView({
        top: 0,
        data: data,
        backgroundColor: 'transparent',
        separatorColor: 'yellow'
    });
    self.add(table);

    function Calc() {
        try {
            var byPass = 0;
            var purchasePrice = parseInt(txtSellingPrice.value);
            if (isNaN(purchasePrice) || purchasePrice === 0) {
                byPass = 1;
            } else {
                var criteria = 180000;
                var delta = 100;
                var _1st = 0;
                var _2nd = 0;
                var _balance = 0;
                var stampDutyValue = 0;
                var _addedValue = 0;
                //Calculate Stamp Duty
                if (purchasePrice < criteria) {
                    _1st = purchasePrice;
                } else {
                    _1st = criteria;
                    if ((purchasePrice - criteria) < criteria) {
                        _2nd = purchasePrice - criteria;
                    } else {
                        _2nd = criteria;
                        _balance = (purchasePrice - 2 * criteria);
                    };
                    stampDutyValue = (_1st / 100) * 1 + (_2nd / 100) * 2 + (_balance / 100) * 3;
                };
                //Calculate  Added Value
                var duration = dateBetween(dateOTP, dateOriginal);
                if (duration > 0) {
                    if (valuePropertyType === 0) {
                        // Date(year, month, day, hours, minutes, seconds, milliseconds)
                        var date1Stone = new Date(2010, 2, 20, 0, 0, 0);
                        var date2Stone = new Date(2010, 8, 30, 0, 0, 0);
                        var date3Stone = new Date(2011, 1, 14, 0, 0, 0);
                        var dayPerYear = 365;
                        if (dateOriginal < date1Stone) {
                            _addedValue = 0;
                        } else {
                            if (date1Stone <= dateOriginal && dateOriginal < date2Stone) {
                                if (duration <= dayPerYear) {
                                    _addedValue = purchasePrice*0.03 - 5400;
                                } else {
                                    _addedValue = 0;
                                };
                            } else {
                                if (date2Stone <= dateOriginal && dateOriginal < date3Stone) {
                                    if (duration <= dayPerYear) {
                                        _addedValue = purchasePrice*0.03 - 5400;
                                    } else {
                                        if (duration <= (2 * dayPerYear)) {
                                            _addedValue = (purchasePrice*0.03 - 5400)*2.0/3;
                                        } else {
                                            if (duration <= (3 * dayPerYear)) {
                                                _addedValue = (purchasePrice*0.03 - 5400)*1.0/3;
                                            } else {
                                                _addedValue = 0;
                                            };
                                        };
                                    };
                                } else {
                                    if (duration <= dayPerYear) {
                                        _addedValue = purchasePrice * 0.16;
                                    } else {
                                        if (duration <= (2 * dayPerYear)) {
                                            _addedValue = purchasePrice * 0.12;
                                        } else {
                                            if (duration <= (3 * dayPerYear)) {
                                                _addedValue = purchasePrice * 0.08;
                                            } else {
                                                if (duration <= (4 * dayPerYear)) {
                                                    _addedValue = purchasePrice * 0.04;
                                                } else {
                                                    _addedValue = 0;
                                                };
                                            };
                                        };
                                    };
                                };
                            };
                        };
                    };
                };
            };
            if (byPass === 0) {
            	lblTotalSellerDuty.text = (purchasePrice + stampDutyValue + _addedValue).toString();
            } else {
            };
        } catch (e) {
            Ti.API.info(e);
        }
    }

    function showKeyBoard(dateIndex) {
        var osname = Ti.Platform.osname;
        var picker = Ti.UI.createPicker({
            type: Ti.UI.PICKER_TYPE_DATE,
            minDate: new Date(2010, 0, 1),
            maxDate: new Date(2099, 11, 31),
            value: new Date(2014, 3, 12)
        });
        if (osname === 'android') {
            picker.showDatePickerDialog({
                value: new Date(2010, 8, 1),
                callback: function (e) {
                    if (e.cancel) {
                        Ti.API.info('User canceled dialog' + dateIndex);
                    } else {
                        solveDate(dateIndex, e.value);
                    }
                }
            });
        } else {
            var fakeView = Titanium.UI.createView({
                backgroundColor: 'transparent',
                top: 0,
                left: 0,
                width: self.width,
                height: self.height
            });
            self.add(fakeView);
            fakeView.addEventListener('click', function (e) {
                self.remove(fakeView);
                self.remove(picker);
            });
            picker.bottom = 0;
            self.add(picker);
            picker.addEventListener('change', function (e) {
                solveDate(dateIndex, e.value);
            });
        };
    }

    function solveDate(dateIndex, dateValue) {
        Ti.API.info('User selected date: ' + dateValue.toLocaleString() + ' ' + dateIndex);
        var dateString = dateFormat(dateValue, 'DD/MM/YYYY');
        if (dateIndex === 1) {
            dateOriginal = dateValue;
            txtOriginalPurchaseDate.value = dateString;
        } else {
            dateOTP = dateValue;
            txtCurrentOtpDate.value = dateString;
        };
        if (isNaN(dateOTP) || isNaN(dateOriginal)) {} else {
            var daysBetween = (Date.parse(dateOTP) - Date.parse(dateOriginal)) / (24 * 3600 * 1000);
            lblDuration.text = parseInt(daysBetween).toString();
        };
    }

    function dateBetween(date1, date2) {
        return parseInt((Date.parse(date2) - Date.parse(date1)) / (24 * 3600 * 1000));
    }

    function dateFormat(date, format) {
        // Calculate date parts and replace instances in format string accordingly
        format = format.replace("DD", (date.getDate() < 10 ? '0' : '') + date.getDate()); // Pad with '0' if needed
        format = format.replace("MM", (date.getMonth() < 9 ? '0' : '') + (date.getMonth() + 1)); // Months are zero-based
        format = format.replace("YYYY", date.getFullYear());
        return format;
    }
    var dateOriginal = null;
    var dateOTP = null;
    return self;
}
module.exports = SellerStampDutyView;